// pages/orderDetails/orderDetails.js
const app = getApp()
var api = require('../../utils/api.js')
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    item:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var oid = options.oid
    this.setData({
      oid: oid
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.orderDetails();
  },
  orderDetails:function(){
    var that = this;
    var oid = this.data.oid;
    api.POST({
      params: { "oid": oid},
      url: app.globalData.base_url + 'couponsorder/info',
      success: function (res) {
        if (util.isEmpty(res.data.code)) {
          var productList = res.data.data.productList;
          var productDetails = res.data.data;
          that.data.item.productList = productList;
          var invoice =  productList.every(function(item){
            return item.return_status==0
          })
          that.setData({
            invoiceCol: invoice
          })
          // var newArr = [];
          // var pickupCode = res.data.data.pickup_code;
          // for (var i = 0; i < pickupCode.length; i++) {
          //   var item = pickupCode.substr(i, 1);
          //   newArr.push(item)
          // }

          var numbers = 0;
          var total_fees = 0;
          // console.log(productList.length);
          for (var i = 0; i < productList.length;i++){
            numbers = numbers / 1 + productList[i].number/1;
            total_fees = total_fees / 1 + productList[i].total_fee / 1;
          }


          that.setData({
            item: that.data.item,
            productDetails: productDetails,
            numbers: numbers,
            total_fees: total_fees
          })
        }
      },
      fail: function (fail) {
        util.showError("登录失败，请检查网络");
      }
    })
  },
  //确认收货
  receiveOrder: function (e) {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '是否确认收货',
      success: function (res) {
        if (res.confirm) {
          api.POST({
            params: { oid: that.data.oid },
            url: app.globalData.base_url + 'order/receipt',
            success: function (res) {
              if (util.isEmpty(res.data.code)) {
                that.onShow();
              } else {
                util.showError(res.data.message);
              }
            },
            fail: function (fail) {
              util.showError("请检查网络");
            }
          })
        }
      }
    })
  },
  //联系客服
  cllphone: function (e) {
    var phone = e.currentTarget.dataset.phone
    wx.makePhoneCall({
      phoneNumber: phone
    })
  },
  refund:function(e){
    var id = e.currentTarget.dataset.sku_code;
    var status = e.currentTarget.dataset.status;
    var oid = this.data.oid;
    if(status==0){
      wx.redirectTo({
        url: '/pages/refund/refund?oid=' + oid + "&&sku_code=" + id
      })
    }else if(status == 1){
      wx.redirectTo({
        url: '../refundSecond/refundSecond?id=' + id
      })
    }else{
      wx.navigateTo({
        url: '../refundSuccessful/refundSuccessful?id=' + id
      })
    }
  },
  // 订单支付
  orderPay: function (e) {
    var that = this;
    var oid = this.data.oid;
    wx.showModal({
      title: '提示',
      content: '是否确认支付',
      success: function (res) {
        if (res.confirm) {

          that.wxPayOrder(oid);
        }
      }
    })
  },

  // 订单支付
  //支付接口
  wxPayOrder: function (oid) {
    var params = new Object()
    var that = this;
    params.oid = oid;
    api.POST({
      params: params,
      url: app.globalData.base_url + 'payment/getWechatInfo',
      success: function (res) {
        if (util.isEmpty(res.data.code)) {


          var preData = res.data.data.payinfo;
          wx.requestPayment({
            timeStamp: preData.timeStamp,
            nonceStr: preData.nonceStr,
            package: preData.package,
            signType: preData.signType,
            paySign: preData.paySign,
            success: function (res) {
              that.onShow();
            },
            fail: function (error) {
              util.showError("支付失败");
            }
          })
        } else {
          util.showError(res.data.message);
        }
      },
      fail: function (fail) {
        util.showError("登录失败，请检查网络");
      }
    })
  },
  //取消取货
  cancelPickUp: function (e) {
    var params = new Object()
    var that = this;
    wx.showModal({
      title: '提示',
      content: '是否取消取货',
      success: function (res) {
        if (res.confirm) {
          api.POST({
            params: { oid: that.data.oid },
            url: app.globalData.base_url + 'order/cancelPick',
            success: function (res) {
              if (util.isEmpty(res.data.code)) {
                that.onShow();
              } else {
                util.showError(res.data.message);
              }
            },
            fail: function (fail) {
              util.showError("请检查网络");
            }
          })
        }
      }
    })
  },
  //打开柜门
  openDoor: function (e) {
    var that = this;
    var oid =this.data.oid;
    wx.showModal({
      title: '提示',
      content: '是否打开柜门取货?',
      success: function (res) {
        if (res.confirm) {
          api.POST({
            params: { oid: oid },
            url: app.globalData.base_url + 'order/pickup',
            success: function (res) {
              if (util.isEmpty(res.data.code)) {
                util.showError("柜门已打开");
                that.onShow();
              } else {
                util.showError(res.data.message);
              }
            },
            fail: function (fail) {
              util.showError("请检查网络");
            }
          })
        }
      }
    })
  },

  //开发票
  getInvoice: function (e) {
    var that = this;
    var oid = this.data.oid;
    wx.navigateTo({
      url: '/pages/invoice/invoice?oid=' + oid
    })
  },
  //预览图片
  //预览图片
  previewImage: function (e) {
    var current = e.target.dataset.src;
    var urls=[]
    urls.push(current)
    wx.previewImage({
      current: current,
      urls: urls
    })
  }

})